package com.mvc.controller.member;
import java.io.File;
import java.util.Calendar;
import java.util.List;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.mvc.controller.base.BaseController;
import com.mvc.framework.constant.GlobalConstant;
import com.mvc.model.base.SessionInfo;
import com.mvc.model.mb.archive.BaseInfo;
import com.mvc.service.mb.UploadHandleService;

/** 图片上传
 * @author hejie
 *
 */

@Controller
@RequestMapping("/upload")
public class UploadHandleController  extends BaseController {
	@Autowired
	private  UploadHandleService uploadService;
	
	@RequestMapping("/uploadpicture")  
	    public String upload(@RequestParam(value = "pic", required = false) MultipartFile file, HttpServletRequest request, ModelMap model) { 
			SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
			if(sessionInfo!=null&&(sessionInfo.getLoginname() != null)){
			List<BaseInfo> bInfo = uploadService.getBaseInfo(sessionInfo.getLoginname());
			//创建储存图片的文件夹upload
	        String path = request.getSession().getServletContext().getRealPath("upload");  
	        //取得上传文件的文件名
	        String fileName = file.getOriginalFilename();   
	        //强制根据随机生成字符串改变上传文件名  避免同名
	        String newfileName =generateRandomFilename()+fileName;
	        File targetFile = new File(path, newfileName);  
	        if(!targetFile.exists()){  
	            targetFile.mkdirs();  
	       }  	  
	       //保存图片到upload文件夹
	        try {  
	            file.transferTo(targetFile);  
		        for(BaseInfo b : bInfo){
					b.setPictureUrl(request.getContextPath()+"/upload/"+newfileName);
					uploadService.updateBaseInfo(b);
				}			
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }
		
	        model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+newfileName);
			}
	        return "/index";
	    }
		
	    /*  
	     * 生成随机文件名  
	     */    
	    public String generateRandomFilename(){    
	        String RandomFilename = "";    
	        Random rand = new Random();//生成随机数    
	        int random = rand.nextInt();    
	            
	        Calendar calCurrent = Calendar.getInstance();    
	        int intDay = calCurrent.get(Calendar.DATE);    
	        int intMonth = calCurrent.get(Calendar.MONTH) + 1;    
	        int intYear = calCurrent.get(Calendar.YEAR);    
	        String now = String.valueOf(intYear) + "_" + String.valueOf(intMonth) + "_" +    
	            String.valueOf(intDay) + "_";    
	       // log.debug("生成于今日的文件名前缀为："+now);                
	        RandomFilename = now + String.valueOf(random > 0 ? random : ( -1) * random);    
	            
	        return RandomFilename;    
    }    
	}


